package com.neu.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neu.dao.UserRoleDao01;
import com.neu.entity.TBUserRole01;
import com.neu.util.JDBCUtil;

public class UserRoleDaoImpl01 implements UserRoleDao01 {
  /**
   * 注册
   */
	@Override
	public boolean registerUsers(TBUserRole01 tr) throws  SQLException {
		Connection conn=null;
		try {
		 conn = JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//   用户管理界面
		String sql = "insert into tb_users_role value(null,?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, tr.getName());
		ps.setString(2, tr.getPass());
		ps.setInt(3, tr.getRole());
		int role =tr.getRole();
		int a = ps.executeUpdate();
		boolean flag = false;
		if(a>0) {
			if(role==1) {	
			String sql_suers = "insert into users value(null,?,'','','','')";
			PreparedStatement ps_suers = conn.prepareStatement(sql_suers);
			ps_suers.setString(1, tr.getName());
			int a_teacher = ps_suers.executeUpdate();
			if(a_teacher>0) {
				flag=true;
			}
		}
			String sql_goods = "insert into student value(null,?,0,0,)";
			PreparedStatement ps_goods = conn.prepareStatement(sql_goods);
			ps_goods.setString(1, tr.getName());
			int a_goods = ps_goods.executeUpdate();
			if(a_goods>0) {
				flag=true;
		}
	}
		return flag;
}
	
			 /**
			  *   登录
			  */
	@Override
	public TBUserRole01 loginValidate(TBUserRole01 tr) throws SecurityException, SQLException {
		Connection conn=null;
		try {
			conn = JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from tb_users_roers where name=? and pass=?";
		TBUserRole01 tr1 = null;
		PreparedStatement ps= conn.prepareStatement(sql);
		ps.setString(1, tr.getName());
		ps.setString(2, tr.getPass());
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			tr1=new TBUserRole01(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4));
		}
		return tr1;
	}

}
